package b.h.y.bj.fragment;

import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

import java.util.ArrayList;

import b.h.y.bj.MainActivity;
import b.h.y.bj.R;
import b.h.y.bj.domain.NewsData;


public class LeftMenuFragment extends BaseFragment {


    private ArrayList<NewsData.NewsMenuData> menuList;

    private int mCurrentPos; //当前被点击的菜单项


    @ViewInject(R.id.lv_list)
    private ListView mlvList;
    private MenuAdapter menuAdapter;


    @Override
    public View initViews() {
        View view = View.inflate(mActivity, R.layout.fragmentleft_menu, null);
        //加入注解
        ViewUtils.inject(this, view);
        return view;
    }

    @Override
    public void initData() {
        mlvList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                mCurrentPos = position;
                menuAdapter.notifyDataSetChanged();

                setCurrentMenuDetailPager(position);

                toggleSlidingMenu(); //隐藏
            }
        });
    }

    /**
     * 切换SlidingMenu的状态
     */
    private void toggleSlidingMenu() {
        MainActivity mainActivity = (MainActivity) mActivity;
        SlidingMenu slidingMenu = mainActivity.getSlidingMenu();
        slidingMenu.toggle(); //切换状态
    }

    /**
     * 设置当前的菜单详情页
     *
     * @param position
     */
    private void setCurrentMenuDetailPager(int position) {
        MainActivity activity = (MainActivity) mActivity;
        ContentFragment contentFragment = activity.getContentFragment(); //获取主页面的fragment
        contentFragment.getNewsCenterPager().setCurrentMenuDetailPager(position); //获取新闻详情页并设置菜单页内容
    }

    /**
     * 设置网络数据
     */
    public void setMenuData(NewsData data) {
        menuList = data.data;
        menuAdapter = new MenuAdapter();
        mlvList.setAdapter(menuAdapter);
    }

    /**
     * 侧边栏的适配器
     */
    class MenuAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return menuList.size();
        }

        @Override
        public NewsData.NewsMenuData getItem(int i) {
            return menuList.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            View v = View.inflate(mActivity, R.layout.list_men_item, null);
            TextView mTvTitle = (TextView) v.findViewById(R.id.tv_title);
            mTvTitle.setText(getItem(i).title);

            if (mCurrentPos == i) {
                mTvTitle.setEnabled(true);
            } else {
                mTvTitle.setEnabled(false);
            }

            return v;
        }
    }
}
